package net.chenlin.dp.modules.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.chenlin.dp.common.annotation.SysLog;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.support.redis.RedisCacheManager;
import net.chenlin.dp.common.utils.CommonUtils;
import net.chenlin.dp.common.utils.MD5Utils;
import net.chenlin.dp.common.utils.ShiroUtils;
import net.chenlin.dp.modules.sys.entity.Login;
import net.chenlin.dp.modules.sys.entity.SysUserEntity;
import net.chenlin.dp.modules.sys.service.SysUserService;
import net.chenlin.dp.modules.uuid.IdUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

/**
 * @author Yangsm
 * @ClassName: AppLogin
 * @Description: TODO
 * @date 2022年10月27日 17:09
 */
@RestController
@RequestMapping("/app")
@Api(tags = "APP登录")
public class AppLogin {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RedisCacheManager redisCacheManager;

    @SysLog("APP登录")
    @ApiOperation(value = "APP登录")
    @PostMapping(value = "/appLogin")
    public R appLogin(@RequestBody Login login) {
        String username = login.getUsername();
        String password = login.getPassword();
        try {

            // 用户名验证
            if (StringUtils.isBlank(username)) {
                //model.addAttribute("errorMsg", "用户名不能为空");
                return R.error("用户名不能为空");
            }
            // 密码验证
            if (StringUtils.isBlank(password)) {
                //model.addAttribute("errorMsg", "密码不能为空");
                return R.error("密码不能为空");
            }
            UsernamePasswordToken token = new UsernamePasswordToken(username, MD5Utils.encrypt(username, password));
            System.out.println(token);
            ShiroUtils.getSubject().login(token);
            SecurityUtils.getSubject().getSession().setAttribute("sessionFlag", true);
            Serializable id = SecurityUtils.getSubject().getSession().getId();
            System.out.println(" ====== sessionId ======"+id);
            SysUserEntity userEntity = sysUserService.getByUserName(username);
            String uuid = IdUtils.fastUUID();
            System.out.println(uuid);
            String newToken =uuid.replaceAll("-", "");
            System.out.println(" ===== "+newToken);
            userEntity.setToken(newToken);
            redisCacheManager.set(userEntity.getToken(), userEntity);
            //redisService.setCacheObject(userEntity.getToken(), userEntity, EXPIRE_TIME, TimeUnit.MINUTES);
            //SysUserEntity user = getUser();
            //System.out.println("登录获取的USER:"+ user);
            return CommonUtils.msg(userEntity);
        } catch (UnknownAccountException | IncorrectCredentialsException | LockedAccountException e) {
            return R.error(e.getMessage());
            //System.out.println(e.getMessage());

            //model.addAttribute("errorMsg", e.getMessage());
        } catch (AuthenticationException e) {
            return R.error("登录服务异常");
            //model.addAttribute("errorMsg", "登录服务异常");
        }
    }

    @SysLog("APP注册账号")
    @ApiOperation(value = "APP注册账号")
    @PostMapping(value = "/appRegister")
    public R appRegister(@RequestBody SysUserEntity user) {
        //user.setUserIdCreate(getUserId());
        return sysUserService.saveUser(user);
    }


    /**
     * APP重置密码
     * @param login
     * @return
     */
    @SysLog("APP重置密码")
    @ApiOperation(value = "APP重置密码")
    @PostMapping("/appReset")
    public R updatePswd(@RequestBody Login login) {
        return sysUserService.updatePswdByApp(login);
    }
}
